<?php

//conecta com o db
include ("./config_bd.php");
//inclui consultas
//include ("src/consultas.php");

if (!isset($_POST['login']) and ! isset($_POST['senha'])) {
    echo " <script>  
        alert('Falta de dados!');
        window.history.back();
        </script>";
    exit;
}

$login = $_POST['login'];
$senha = md5($_POST['senha']);


//echo " email: " . $email;
//echo " Senha: " . $senha;

$sql = "SELECT f.idfuncionario as id, f.nome as nome, f.login as login, f.senha as senha, 'funcionario' as tipo
        FROM funcionario f
        WHERE f.login='$login' AND f.senha='$senha'
        
        UNION
        
        SELECT c.idcliente as id, c.nome as nome, c.login as login, c.senha as senha, 'cliente' as tipo
        FROM cliente c
        WHERE c.login='$login' AND c.senha='$senha'";

$consulta = mysql_query($sql) or die("ERROR");


if (strlen($login) < 1 or strlen($senha) < 1) {
    echo " <script>  
        alert('Falta de dados!');
        window.history.back();
        </script>";
} else if (mysql_num_rows($consulta) > 0) {

    while ($result = mysql_fetch_array($consulta)) {
        session_start();
        $_SESSION['id'] = $result['id'];
        $_SESSION['nome'] = $result['nome'];
        $_SESSION['login'] = $result['login'];        
        $_SESSION['tipo'] = $result['tipo'];
    }

    echo "Login efetuado";
    $_SESSION['logado'] = 1;

//    include ('src/avaliacao.php');

    //avalia($_SESSION['id']);
    //echo "<br/>" . $_SESSION['email'] . "<br/>" . $_SESSION['nome'] ;
    header("Location: inicio.php");
} else {
    echo "
        <script> 
        alert('Dados incorretos!');
        window.history.back();
        </script>";
}
?>
